package com.zbg;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

/**
 * @program: servlet
 * @description:
 * @author: Dawson.Zhang
 * @create: 2020-03-07
 **/
public class ServletReq extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setAttribute("userName","张保国");
        RequestDispatcher requestDispatcher = req.getRequestDispatcher("/servlet2");
        System.out.println("柜台一结束==跳转至柜台二");
        requestDispatcher.forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println(req.getRequestURI()+"=req.getRequestURI()");
        System.out.println(req.getRequestURL()+"=req.getRequestURL()");
        System.out.println(req.getRemoteHost()+"=req.getRemoteHost()");
        System.out.println(req.getHeader("User-Agent")+"=req.getHeader()");
        System.out.println(req.getMethod()+"=req.getMethod()");
        System.out.println("-------------------------------------------");
        //设置编码要在获取请求参数之前使用，否则无效
        req.setCharacterEncoding("UTF-8");
        System.out.println(req.getParameter("userName"));
        System.out.println(req.getParameter("pwd"));
        System.out.println(Arrays.toString(req.getParameterValues("inter")));
        System.out.println(req.getParameter("addr"));
        System.out.println("-------------------------------------------");
        req.setAttribute("userName","张保国");
        RequestDispatcher requestDispatcher = req.getRequestDispatcher("/servlet2");
        System.out.println("柜台一结束==跳转至柜台二");
        requestDispatcher.forward(req,resp);
    }
}
